package com.sjz.rydemo

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.sjz.rydemo.databinding.FrgmLoginBinding
import com.sjz.rydemo.net.Common
import com.sjz.rydemo.net.HttpUtils
import com.sjz.rydemo.net.TextCallBack
import com.sjz.rydemo.utils.LogUtil
import com.sjz.rydemo.utils.showToast

class LoginFrgm : Fragment() {

    private var _binding: FrgmLoginBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FrgmLoginBinding.inflate(inflater, container, false)
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        binding.loginButton.setOnClickListener {
            login()
        }
    }


    /**
     * 登录
     */
    private fun login() {
        if (TextUtils.isEmpty(binding.editUsername.text.toString())) {
            "请输入您的手机号".showToast(requireContext())
            return
        }
        if (TextUtils.isEmpty(binding.editVcode.text.toString())) {
            "请输入验证码".showToast(requireContext())
            return
        }
        if (!binding.agreeCheckbox.isChecked) {
            "请阅读并同意用户协议".showToast(requireContext())
            return
        }

        loginByNetwork(binding.editUsername.text.toString(), binding.editVcode.text.toString())

    }

    private fun loginByNetwork(username: String, sms: String) {
        val params = HashMap<String, Any>()
        params["phone"] = username
        params["code"] = sms
        HttpUtils.getResult(Common.APP_LOGIN_SMS, params, object : TextCallBack<String>() {
            override fun onFailure(e: ResponseException) {
                LogUtil.e("响应结果", "==========$e")
            }

            override fun onSuccess(text: String) {
                LogUtil.e("响应结果", "==========$text")
            }
        })
    }
}